package com.bd03.streaminglearn.day0325

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

object NetworkWordCount {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
    val ssc = new StreamingContext(conf, Seconds(2)) //Seconds是批次时间
    //通过ssc直接调出来的数据源是基础数据源
    //使用ssc监听hdp01机器的9999端口上源源不断的数据
    //tail -f tail -F   a a a a  b b
    val data: ReceiverInputDStream[String] = ssc.socketTextStream("hdp01",9999)
    val res: DStream[(String, Int)] = data.flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_)
    //统计的是分批次的结果,如果想获取带历史状态结果?
    //只统计了当前批次的数据
    //如果想获取带历史状态的结果 ,
    // 可以把数据写入到mysql ,hive,   redis,基于内存的,快
    //streaming也为我们提供了一个带历史状态的统计,updateStateByKey,checkpoint
    res.print()

    ssc.start()             // Start the computation
    ssc.awaitTermination()  // Wait for the computation to terminate   阻塞状态,等待着被终止

  }

}
